<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="4">
					<a-form-item label="推荐分公司" name="city">
						<a-select
							v-model:value="searchFormState.city"
							placeholder="请选择推荐分公司"
							:options="branchCityOptions"
							:allowClear="true"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="候选人姓名" name="candidateName">
						<a-input v-model:value="searchFormState.candidateName" placeholder="请输入候选人姓名" />
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="推荐部门" name="recommendedDepartment">
						<DeptSelectTree
							v-model="searchFormState.recommendedDepartment"
							ref="deptSelectRef"
							placeholder="请选择推荐部门"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="简历等级" name="resumeLevel">
						<a-select
							v-model:value="searchFormState.resumeLevel"
							placeholder="请选择简历等级"
							:options="resumeLevelOptions"
							allowClear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="简历来源" name="resumeSource">
						<a-select
							v-model:value="searchFormState.resumeSource"
							placeholder="请选择简历来源"
							:options="resumeSourceOptions"
							allowClear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table ref="tableRef" :columns="columns" :data="loadData" bordered rowKey="id" :scroll="{ x: 'max-content' }">
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()">
						<template #icon>
							<plus-outlined />
						</template>
						新增
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a-button type="link" @click="formRef.onOpen(record)">编辑</a-button>
						<a-button type="link" @click="interviewRef.onOpen(record, 'scheduling')">预约面试</a-button>
						<a-button type="link" @click="interviewRef.onOpen(record, 'result')"> 面试结果</a-button>
						<!-- 录用跟进按钮：面试通过后显示 -->
						<a-button type="link" @click="interviewRef.onOpen(record, 'followup')"> 录用跟进</a-button>
						<a-button v-if="record.resumeAttachment" type="link" @click="downloadResume(record)"> 下载简历</a-button>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="handleFormSuccess" />
	<Interview ref="interviewRef" @successful="tableRef.refresh(true)" />
</template>

<script setup name="RecruitmentProfile">
	import { ref } from 'vue'
	import tool from '@/utils/tool'
	import Form from './form.vue'
	import Interview from './interview.vue'
	import recruitmentProfileApi from '@/api/hrms/recruitment/profile/recruitmentProfileApi'
	import DeptSelectTree from '@/components/Selector/deptSelectTree.vue'
	import fileApi from '@/api/tjzy/file/fileApi'
	import downloadUtil from '@/utils/downloadUtil'

	const searchFormState = ref({
		current: 1,
		size: 10,
		candidateName: undefined,
		recommendedDepartment: undefined,
		resumeLevel: undefined,
		resumeSource: undefined
	})

	const tableRef = ref()
	const searchFormRef = ref()
	const formRef = ref()
	const interviewRef = ref()
	const branchCityOptions = tool.dictList('BRANCH_CITY')
	const resumeLevelOptions = tool.dictList('RESUME_LEVEL')
	const resumeSourceOptions = tool.dictList('RESUME_SOURCE')

	const columns = [
		{ title: '候选人姓名', dataIndex: 'candidateName' },
		{ title: '推荐分公司', dataIndex: 'cityName' },
		{ title: '推荐职位', dataIndex: 'recommendedPositionCodeName' },
		{ title: '推荐部门', dataIndex: 'recommendedDepartmentName' },
		{ title: '推荐项目', dataIndex: 'recommendedProject' },
		{ title: '简历等级', dataIndex: 'resumeLevelName' },
		{ title: '简历来源', dataIndex: 'resumeSourceName' },
		{ title: '推荐人', dataIndex: 'recommenderName' },
		{ title: '面试时间', dataIndex: 'interviewTime' },
		{ title: '面试方式', dataIndex: 'interviewMethodName' },
		{ title: '面试官', dataIndex: 'interviewerName' },
		{ title: '核心面试官', dataIndex: 'coreInterviewerName' },
		{ title: '面试结果', dataIndex: 'interviewResultName' },
		{ title: '面试反馈意见', dataIndex: 'interviewFeedback' },
		{ title: '录用跟进结果', dataIndex: 'hiringResultName' },
		{ title: '入职时间', dataIndex: 'onboardingDate' },
		{ title: '录用备注', dataIndex: 'remark' },
		{ title: '操作', dataIndex: 'action', align: 'center', width: 200, fixed: 'right' }
	]

	// 加载数据
	const loadData = async (params) => {
		return await recruitmentProfileApi.pageList(searchFormState.value)
	}

	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}

	// 表单操作成功后的回调
	const handleFormSuccess = () => {
		tableRef.value.refresh(true)
	}

	// 下载简历
	const downloadResume = (record) => {
		const arr = record.resumeAttachment.split('/')
		fileApi.downloadWithBucket(arr[0], arr[1]).then((res) => {
			downloadUtil.resultDownload(res)
		})
	}
</script>
